<?php

namespace app\beautiful\controller\admin\miniapp;

use app\beautiful\model\StyleModel;

use app\BaseController;

use app\beautiful\logic\ConDataLogic;

class Template extends BaseController{

    public function getStyle(){

        $style = StyleModel::find($this->request->shopId);

        if(null === $style){

            $return['style'] = null;

        }else{

            $datas = $style->toArray();

            $return['style'] = $datas['style'];

        }

        return $this->success($return);

    }

    public function saveStyle(){

        $style = $this->request->param('style','', 'security_json_or_array');

        if(empty($style)){

            return $this->error('请提交风格');

        }

        if(null === $styleDatas = StyleModel::find($this->request->shopId)){

            $styleDatas = new StyleModel();

            $styleDatas->shop_id = $this->request->shopId;

        }

       

        $styleDatas->style = $style;

        $styleDatas->save();

        cache('miniapp_style' . $this->request->shopId,null);

        return $this->success('保存成功');

    }

    

    public function getHomeData()

    {

        $params=$this->request->param('params/a',[]);

        if(empty($params)) return $this->success([]);

    

        foreach ($params as $param){

            if(!isset($param['key'])) return $this->error('参数错误');

            if(empty($param['key'])) return $this->error('参数错误');

            if(!isset($param['num'])) return $this->error('参数错误');

            if(empty($param['num']) || $param['num']<=0) return $this->error('参数错误');

        }



        $ConDataLogic = new ConDataLogic($this->request->shopId,0);

        $datas=$ConDataLogic->getData($params);

        

        return $this->success(['datas'=>$datas]);

    }

}

